import { createRouter, createWebHashHistory } from 'vue-router'
import type { RouteRecordRaw } from 'vue-router'
import type { App } from 'vue'
import { useI18n } from '@/hooks/web/useI18n'
import { BasicLayout, Layout, loadModule, formatRoutes } from '@/utils/routeTools'
const { t } = useI18n()

export const constantRoutes: AppRouteRecordRaw[] = [
  {
    path: '/',
    redirect: '/loading',
    name: 'Root',
    meta: {
      hidden: true
    },
    children: [
      {
        path: '/loading',
        component: () => import('@/views/Loading.vue'),
        name: 'Loading',
        meta: {
          hidden: true,
          title: t('router.loading'),
          noTagsView: true
        }
      }
    ]
  },
  {
    path: '/home',
    redirect: '/home',
    component: BasicLayout,
    name: 'Home',
    meta: {
      hidden: true
    },
    children: [
      {
        path: '/home/index',
        component: () => import('@/views/Home/index.vue'),
        name: 'HomeIndex',
        meta: {
          hidden: true,
          title: t('router.home'),
          noTagsView: true
        }
      }
    ]
  },
  {
    path: '/login',
    component: () => import('@/views/Login/Login.vue'),
    name: 'Login',
    meta: {
      hidden: true,
      title: t('router.login'),
      noTagsView: true
    }
  },
  {
    path: '/404',
    component: () => import('@/views/Error/404.vue'),
    name: 'NoFind',
    meta: {
      hidden: true,
      title: '404',
      noTagsView: true
    }
  },
  {
    path: '/dept',
    component: Layout,
    redirect: '/dept',
    children: [
      {
        path: '/dept/change',
        component: () => import('@/views/dept/deptChange.vue'),
        name: 'deptChange',
        meta: { title: '单位部门变动', affix: false, hidden: true }
      }
    ],
    name: 'HrDept',
    meta: undefined
  },
  {
    path: '/emp',
    component: Layout,
    redirect: undefined,
    children: [
      {
        path: '/emp/add',
        component: () => import('@/views/emp/editEmp.vue'),
        name: 'addEmp',
        meta: { title: '添加员工', affix: false, hidden: true }
      },
      {
        path: '/emp/edit/:id',
        component: () => import('@/views/emp/editEmp.vue'),
        name: 'editEmp',
        meta: { title: '编辑员工资料', affix: false, hidden: true }
      },
      {
        path: '/emp/view/:id',
        component: () => import('@/views/emp/viewEmp.vue'),
        name: 'showEmp',
        meta: { title: '查看员工资料', affix: false, hidden: true }
      }
    ],
    name: 'HrEmp',
    meta: null
  }
]

const createRoute = () => {
  const routes = loadModule(constantRoutes)
  return createRouter({
    history: createWebHashHistory(),
    strict: true,
    routes: routes,
    scrollBehavior: () => ({ left: 0, top: 0 })
  })
}

const router = createRoute()

export const resetRouter = (): void => {
  const resetWhiteNameList = ['Loading', 'Login', 'NoFind', 'Root']
  router.getRoutes().forEach((route) => {
    const { name } = route
    if (name && !resetWhiteNameList.includes(name as string)) {
      router.hasRoute(name) && router.removeRoute(name)
    }
  })
}

export const setupRouter = (app: App<Element>) => {
  app.use(router)
}

export const loadRoute = (routes): AppCustomRouteRecordRaw[] => {
  const list = formatRoutes(routes, 0)
  list.forEach((r) => {
    router.addRoute(r as RouteRecordRaw)
  })
  return list
}
export const addRoutes = (routes: AppCustomRouteRecordRaw[]): void => {
  routes.forEach((r) => {
    router.addRoute(r as RouteRecordRaw)
  })
}
export default router
